function BHVR_Plot2AFC_PsychoFcn(ax,sch)
% BHVR_Plot2AFC_PsychoFcn(ax,sch)
% 
% Plot Psychometric Function
% 
% DJS (2012)
rv = sch.response_vals;

rcidx = regexp(sch.readparams,'response_code');
rcidx = findincell(rcidx) + 1;
slidx = regexp(sch.readparams,'StimLevel');
slidx = findincell(slidx) + 1;

vals   = rv(:,rcidx); vals(isnan(vals))     = [];
levels = rv(:,slidx); levels(isnan(levels)) = [];

wslidx = regexp(sch.writeparams,'StimLevel');
wslidx = findincell(wslidx);
ulevels = unique(cell2mat(sch.trials(:,wslidx)));

% hits = bitget(vals,6);

% LEFT: bit 3(2) ; RIGHT: bit 4(3)
% find # responses to the right feeder
RespRight = zeros(size(ulevels));
NumTrials = zeros(size(ulevels));
for i = 1:length(ulevels)
    ind = levels == ulevels(i);
    RespRight(i) = sum(ind & bitget(vals,3));
    NumTrials(i) = sum(ind);
end
NumTrials(~NumTrials) = nan;

if sum(RespRight) == 0, return; end

cla(ax)
plot(ax,ulevels,RespRight./NumTrials,'-o', ...
    'linewidth',2,'markersize',5);
grid(ax,'on')
xlabel(ax,'Stim Level');  
ylabel(ax,'%Response to Right');
box(ax,'on')
